<template>
  <el-container>
    <el-header>
      <div class="logo-box">
        <img src="../assets/img/heima.png" alt>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :style="!isCollapse?'width:200px;':'width:65px'">
        <el-row class="tac">
          <el-col :span="24">
            <div class="menuToggle" @click="isCollapse=!isCollapse" :style="!isCollapse?'width:200px;':'width:65px'">|||</div>
            <el-menu
              default-active="2"
              background-color="#333744"
              text-color="#fff"
              active-text-color="#409EFF"
              :unique-opened="true"
              :collapse="isCollapse"
              :collapse-transition="false"
              :router="true"
            >
              <el-submenu v-for="(v,i) in menuList" :key="v.id" :index="v.id+''"  :style="!isCollapse?'width:200px;':'width:65px'">
                <template slot="title">
                  <i :class="'iconfont icon-'+menusIcons[i]"></i>
                  <span>{{v.authName}}</span>
                </template>
                <el-menu-item v-for="vv in v.children" :key="vv.id" :index="vv.path">
                  <i class="el-icon-menu"></i>
                  <span>{{vv.authName}}</span>
                </el-menu-item>
              </el-submenu>
            </el-menu>
          </el-col>
        </el-row>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      menuIcons: ['', '', '', '', ''],
      menusIcons: ['users', 'tijikongjian', 'shangpin', 'danju', 'baobiao'],
      isCollapse: false
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    logout() {
      this.$confirm('确定要退出系统么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          window.sessionStorage.removeItem('token')
          this.$router.push('/login')
        })
        .catch(() => {})
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error('获取左侧权限列表失败')
      }
      console.log(res)
      this.menuList = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    background-color: rgb(55, 61, 65);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0;
    padding-right: 20px;
    .logo-box {
      display: flex;
      align-items: center;
      color: white;
      font-size: 22px;
      user-select: none;
      img {
        width: 50px;
        height: 50px;
        margin-right: 10px;
      }
    }
  }
  .el-aside {
    background-color: rgb(51, 55, 68);
  }
  .el-main {
    background-color: rgb(234, 237, 241);
  }
  i.iconfont {
    margin-right: 10px;
  }
  .menuToggle {
    background-color: rgb(74, 80, 100);
    height: 25px;
    text-align: center;
    color: white;
    font-size: 12px;
    line-height: 25px;
    letter-spacing: 0.1em;
    user-select: none;
    cursor: pointer;
  }
}
</style>
